<?php


namespace app\common\model;


use app\common\service\PlatFormFactory;
use think\facade\Db;
use think\Model;
use think\model\relation\HasMany;
use think\model\relation\HasOne;

class Users extends Model
{
    protected $pk = "user_id";
    // 定义时间戳字段名
    protected $createTime = 'reg_time';
    //修改format类型
    protected $dateFormat = "Y-m-d H:i:s";
    protected $type = [
        'user_money'     => 'float',
        'jifen'          => 'float',
        'mcbt'           => 'float',
        'reserve_money'  => 'float',
        'td_yeji'        => 'float',
        'lj_box_num'     => 'float',
        'lj_ry_yeji'     => 'float',
        'day_ry_yeji'    => 'float',
        'day_td_ry_yeji' => 'float',
        'td_month_yeji'  => 'float',
        'month_ry_yeji'  => 'float',
        'td_ry_yeji'     => 'float',
    ];

    public function getPkAttr()
    {
        return $this->pk;
    }

    public function getStatusTextAttr($value, $data)
    {
        $status = [0 => '正常', 1 => '禁用'];
        return $status[$data['is_lock']];
    }

    public function getLevelNameAttr($value, $data)
    {
        return Db::name('user_level')->where('level_id', $data['level'])->value('name');
    }

    public function dengji()
    {
        return $this->hasOne(UserLevel::class, 'level_id', 'level');
    }

    public function store()
    {
        return $this->hasOne(Store::class, 'user_id', 'user_id');
    }

    public function pick()
    {
        return $this->hasOne(Pick::class, 'user_id', 'user_id');
    }

    public function getHeadPicAttr($value)
    {
        if (strpos($value, 'http') === false) {
            return SITE_URL . $value;
        }
        return $value;
    }

    //认养数量
    public function getRyNumAttr($value, $data)
    {
        $where['user_id'] = $data['user_id'];
        $where['order_status'] = 1;
        return FosterOrder::where($where)->count();
    }

    //认养金额
    public function getRyMoneyAttr($value, $data)
    {
        $where['user_id'] = $this->user_id;
        $where['order_status'] = 1;
        return FosterOrder::where($where)->sum('order_amount');
    }

    //判断是否可进行提现
    public function getCanWithAttr($value, $data)
    {
        //要求复购金额
        $fg_amount = Db::name('user_level')->where('level_id', $data['level'])->value('fg_amount');
        if ($fg_amount == 0) {
            return true;
        }
        $end = strtotime('+1 year', $data['valid_time']);
        if (time() <= $end) {//1年内 不用判断 可以提现
            return true;
        } else {
            $where[] = ['user_id', '=', $this->user_id];
            $where[] = ['order_status', '>', 0];
            $start = strtotime('-1 year');
            $end = strtotime('+1 day');
            $start_time = date('Y-m-d', $start);
            $end_time = date('Y-m-d', $end);
            $order_amount = Db::name('order')->where($where)
                ->whereBetweenTime('add_time', $start_time, $end_time)
                ->sum('order_amount');
            if ($order_amount >= $fg_amount) {
                return true;
            }
            return $fg_amount;
        }
    }

    public function third(): HasMany
    {
        return $this->hasMany(ThirdUser::class, 'uid');
    }

    public function thirdWechat(): HasOne
    {
        return $this->hasOne(ThirdUser::class, 'uid')->where('platform', PlatFormFactory::WECHAT);
    }

    public function thirdByte(): HasOne
    {
        return $this->hasOne(ThirdUser::class, 'uid')->where('platform', PlatFormFactory::BYTE);
    }


    public static function onBeforeInsert(Model $model)
    {
        $model->reg_time = time();
        $model->head_pic = 'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/images/3.png';
    }

    public static function onAfterInsert(Model $model): void
    {
        //增加团队人数
        td_all_num($model->getKey());
    }
}
